<template>
  <!-- 
    使用说明：
    1）、引入category-cascader.vue
    2）、语法：<category-cascader :catelogPath.sync="catelogPath"></category-cascader>
        解释：
          catelogPath：指定的值是cascader初始化需要显示的值，应该和父组件的catelogPath绑定;
              由于有sync修饰符，所以cascader路径变化以后自动会修改父的catelogPath，
              这是结合子组件this.$emit("update:catelogPath",v);做的
          -->
  <div>
    <el-cascader
      filterable
      clearable
      placeholder="试试搜索：手机"
      v-model="paths"
      :options="categorys"
      :props="setting"
    ></el-cascader>
  </div>
</template>

<script>
import CategoryApi from "@/api/product/category";

export default {
  props: {
    catelogPath: {
      type: Array,
      default() {
        return [];
      },
    },
  },
  data() {
    return {
      setting: {
        value: "id",
        label: "name",
        children: "children",
      },
      categorys: [],
      paths: this.catelogPath,
    };
  },
  watch: {
    catelogPath(v) {
      this.paths = this.catelogPath;
    },
    paths(v) {
      this.$emit("update:catelogPath", v);
    },
  },
  methods: {
    getCategorys() {
      CategoryApi.getDataList().then(({ data }) => {
        this.categorys = data;
      });
    },
  },
  created() {
    this.getCategorys();
  },
};
</script>
